
package com.lzx.adx.report.controller.v1.youyou;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.lzs.adx.commmon.utils.JsonUtils;
import com.lzs.adx.commmon.utils.Result;
import com.lzx.adx.report.bean.mapper.ReportMapper;
import com.lzx.adx.report.cache.AccountLocalCache;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.model.youyou.YouYouRequest;
import com.lzx.adx.report.service.ReportService;
import com.lzx.adx.report.util.AdPerUtil;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import com.lzx.adx.report.util.UrlUtil;
import com.lzx.adx.report.util.helper.YiduiHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * 媒体：优优
 * 产品 伊对
 */
@RequestMapping("/yy/jym")
@RestController
@Slf4j
public class YouYouJymReportController {


    @Autowired
    private ReportService reportService;

    @Autowired
    private ThreadPoolTaskExecutor reportExecutor;

    @Autowired
    private ReportMapper reportMapper;


    @GetMapping("/report")
    public ResponseEntity report(YouYouRequest youYouRequest) {
        log.info("接收到优优到金易木的请求参数为{}", JsonUtils.toJsonString(youYouRequest));
        //1.收到监测的数据存储
        ReportDTO reportDTO = reportMapper.toReportDTO(youYouRequest);
        reportDTO.setBizRequestId(IdUtil.fastSimpleUUID()+reportDTO.getDspId());
        reportDTO.setCreateTime(new Date());
        reportDTO.setUpdateTime(new Date());
        reportDTO.setCallback(youYouRequest.getCallbackUrl());
        reportDTO.setExtend(JsonUtils.toJsonString(youYouRequest));
        reportDTO.setTraceTime(String.valueOf(System.currentTimeMillis() / 1000));
        reportExecutor.execute(() -> {
            reportService.saveData(reportDTO);
        });
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(youYouRequest.getDspId());
        String finalUrl = UrlUtil.covertYYJymUrl(accountDTO, reportDTO, youYouRequest);
        log.info("账号为{} 完整的优优到金易木请求地址为{}", youYouRequest.getDspId(), finalUrl);
        //2.2夸克上报接口
        String body = HttpUtil.createGet(finalUrl).execute().body();
        log.info("账号为{} 完整的优优到金易木上报的接口，响应的结果为{}", youYouRequest.getDspId(), body);
        return ResponseEntity.ok(new Result());
    }



    /**
     * 夸克回传
     *
     * @param clickId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/soul/callback")
    public ResponseEntity soulCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-soul回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-soul回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("夸克回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}soul回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}soul回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }


    /**
     * 安居客回传
     *
     * @param clickId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/ajk/callback")
    public ResponseEntity ajkCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-安居客 回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-安居客回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("夸克回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}优优-安居客回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}优优-安居客回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }


    /**
     * 优优-智联招聘
     *
     * @param clickId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/zlzp/callback")
    public ResponseEntity zlzpCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-智联招聘回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-智联招聘回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("夸克回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}优优-智联招聘回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}优优-智联招聘回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }



    @GetMapping("/qm/callback")
    public ResponseEntity qmCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-七猫回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-七猫回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("夸克回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}七猫回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}七猫回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }







    @GetMapping("/kkmh/callback")
    public ResponseEntity kkmhCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-快看漫画回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-快看漫画回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("优优-快看漫画回调信息回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}快看漫画回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}快看漫画回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }






    @GetMapping("/hlj/callback")
    public ResponseEntity hljCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-婚礼纪回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-婚礼纪回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("优优-婚礼纪回调信息回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}婚礼纪回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}婚礼纪回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }



    @GetMapping("/xyym/callback")
    public ResponseEntity xyymCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-新氧医美回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-新氧医美回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("优优-新氧医美回调信息回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}新氧医美回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}新氧医美回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }


    @GetMapping("/bbs/callback")
    public ResponseEntity bbsCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-宝宝树回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-宝宝树回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("优优-宝宝树回调信息回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}宝宝树回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}宝宝树回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }

    @GetMapping("/aixzy/callback")
    public ResponseEntity aixzyCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-AI写作猿回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-AI写作猿回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("优优-AI写作猿回调信息回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}AI写作猿回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}AI写作猿回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }


    @GetMapping("/bdzz/callback")
    public ResponseEntity bdzzCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-百度主站回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-百度主站回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("优优-百度主站回调信息回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}百度主站回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}百度主站回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }

    @GetMapping("/xb/callback")
    public ResponseEntity xbCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-学宝回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-学宝回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("优优-学宝回调信息回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}学宝回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}学宝回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }



    @GetMapping("/bdwk/callback")
    public ResponseEntity bdwkCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-百度文库回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-百度文库回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("优优-百度文库回调信息回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}百度文库回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}百度文库回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }





    @GetMapping("/kszz/callback")
    public ResponseEntity kszzCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-快手主站回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-快手主站回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("优优-百度文库回调信息回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}快手主站回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}快手主站回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }










    @GetMapping("/ypw/callback")
    public ResponseEntity ypwCallBackUrl(String clickId) throws Exception {
        String eventType="1";
        log.info("优优-鱼泡网回调信息，请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("优优-鱼泡网回调信息,请求流水号空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("优优-鱼泡网回调信息回传流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //2.从缓存加载配置
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }
        String transformType = jsonObject.getString("transformType");
        //3，获取回传地址，进行解码
        String decodeUrl = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        //拼接完整的请求参数
        Map<String, Object> paramsReq = new HashMap<>();
        paramsReq.put("transformType", transformType);
        String finalUrl = decodeUrl + "&" + URLUtil.buildQuery(paramsReq, StandardCharsets.UTF_8);
        log.info("dspId{},请求流水号{}鱼泡网回调优优的完整地址为：{}", reportDTO.getDspId(), clickId, finalUrl);
        String result = HttpUtil.get(finalUrl);
        log.info("dspId{},请求流水号{}鱼泡网回调优优的结果为：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }




    
}
